﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.Model;
using System.ServiceModel;
using CMS.Contracts;


namespace Presentation
{
    public partial class Update : System.Web.UI.Page
    {
        private IProjectBService channelObject;
        private ChannelFactory<IProjectBService> ChannelFactory = new ChannelFactory<IProjectBService>("CMSClient");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Department> depList = new List<Department>();

                channelObject = ChannelFactory.CreateChannel();
                using (channelObject as IDisposable)
                {
                    depList = channelObject.GetAllDepartments();

                    DropDownListDepartment.Items.Clear();
                    DropDownListDepartment.Items.Capacity = depList.Count;
                    DropDownListDepartment.Items.Insert(0, "");

                    foreach (var a in depList)
                    {
                        ListItem b = new ListItem(a.DepartmentName);
                        if (!DropDownListDepartment.Items.Contains(b))
                        {
                            DropDownListDepartment.Items.Insert(a.DepartmentId, a.DepartmentName);
                            
                        }

                    }

                    string id = Request["ID"];
                    int teacherId = Convert.ToInt32(id);
                    List<Teacher> teacherList = new List<Teacher>();
                    teacherList = channelObject.GetTeacherById(teacherId);

                    foreach (var a in teacherList)
                    {
                        TeacherNameBox.Text = a.TeacherName;

                        List<Department> dList = new List<Department>();
                        depList = channelObject.GetDepartmentById(a.DepartmentId);

                        foreach (var b in depList)
                        {
                            DropDownListDepartment.SelectedValue = b.DepartmentName;
                        }

                        if (a.TeacherGender.Trim().Equals("Male"))
                        {
                            RadioButtonList1.SelectedIndex = 0;
                        }
                        else
                        {
                            RadioButtonList1.SelectedIndex = 1;
                        }
                    }

                    
                }


            }
        }

        protected void Button_Update_Click(object sender, EventArgs e) //return to teacher list page
        {
            string id = Request["ID"];
            //string id=Session["Id"].ToString();
            int TeacherID = Convert.ToInt32(id);
            string str = null;

            List<Department> depList = new List<Department>();
            channelObject = ChannelFactory.CreateChannel();
            using (channelObject as IDisposable)
            {
                depList = channelObject.GetDepartmentById(DropDownListDepartment.SelectedIndex);
                Department myDep = new Department();
                foreach (var b in depList)
                {
                    myDep = b;
                }

                if (0 == RadioButtonList1.SelectedIndex)
                {
                    str = "Male";
                }
                else
                {
                    str = "Female";
                }

                if (0 == DropDownListDepartment.SelectedIndex)
                {
       
                    Response.Redirect("UpdateTeacher.aspx?id=" + id);
                   
                }
                Teacher a = new Teacher
                {
                    TeacherId = TeacherID,
                    TeacherName = TeacherNameBox.Text,
                    TeacherGender = str,
                    DepartmentId = DropDownListDepartment.SelectedIndex,
                    Department = myDep
                };


                try
                {
                    channelObject.UpdateTeacher(a);
                    Response.Redirect("TeacherList.aspx");
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('ex.Message');window.location.href='TeacherList.aspx';</script>");
                }

                Response.End();


            }
        }
    }
}